package com.cmccgz.udacs_landlord_webapp.main.device.presenter;

import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.data.DevicesDataSource;
import com.cmccgz.udacs_landlord_webapp.data.DevicesRepository;
import com.cmccgz.udacs_landlord_webapp.main.device.contract.DeviceContract;

import java.util.List;


public class DevicesPresenter implements DeviceContract.Presenter {
    private final DevicesRepository devicesRepository;
    private DeviceContract.View view;

    public DevicesPresenter(DeviceContract.View view) {
        this.view = view;
        devicesRepository = new DevicesRepository();
    }

    @Override
    public void getDevices() {
        devicesRepository.getDevices(new DevicesDataSource.OnGetDevicesLisenter() {
            @Override
            public void getDevicesCache(List<DeviceInfoBean> datas) {
                view.getDevicesCache(datas);
            }

            @Override
            public void getDevicesSuccess(List<DeviceInfoBean> datas) {
                view.getDevicesSuccess(datas);
            }

            @Override
            public void getDevicesError(String msg) {
                view.getDevicesError(msg);
            }
        });
      /*  List<DeviceInfoBean> deviceList = AppConfig.getDeviceList();
        view.getDevicesCache(deviceList);
        ApiService.deviceSync().subscribe(new Subscriber<ResultBean<List<DeviceInfoBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.getDevicesError("获取列表失败！");
            }

            @Override
            public void onNext(ResultBean<List<DeviceInfoBean>> listResultBean) {
                if (listResultBean!=null
                        &&listResultBean.rsmsg!=null
                        &&"0".equals(listResultBean.rsmsg.rscode)
                        &&listResultBean.resp!=null) {
                    view.getDevicesSuccess(listResultBean.resp);
                }else{
                    String errorMsg=listResultBean!=null
                            &&listResultBean.rsmsg!=null
                            && !StringUtils.isEmpty(listResultBean.rsmsg.rsdesc)
                            ?listResultBean.rsmsg.rsdesc
                            :"获取列表失败！";
                    view.getDevicesError(errorMsg);
                }
            }
        });*/
    }
}
